/*
 * Copyright 2020-2021 redragon.dongbin
 *
 * This file is part of redragon-erp/赤龙ERP.

 * redragon-erp/赤龙ERP is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.

 * redragon-erp/赤龙ERP is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with redragon-erp/赤龙ERP.  If not, see <https://www.gnu.org/licenses/>.
 */
package com.erp.prod.wip.controller;

import java.util.List;
import java.util.Map;

import javax.validation.Valid;

import org.apache.commons.lang.StringUtils;
import org.redragon.framework.hibernate.data.SnowFlake;
import org.redragon.framework.hibernate.model.Pages;
import org.redragon.framework.springmvc.ControllerSupport;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.erp.common.inv.input.dao.model.ProdInvInputLine;
import com.erp.common.inv.input.service.InvInputHeadService;
import com.erp.common.inv.output.dao.model.ProdInvOutputLine;
import com.erp.common.inv.output.service.InvOutputHeadService;
import com.erp.dataset.service.DatasetCommonService;
import com.erp.masterdata.common.service.MasterDataCommonService;
import com.erp.masterdata.material.dao.model.MdMaterial;
import com.erp.prod.procedure.dao.model.ProdWorkProcedure;
import com.erp.prod.procedure.service.ProdWorkProcedureService;
import com.erp.prod.wip.dao.model.ProdWipTaskMaterial;
import com.erp.prod.wip.dao.model.ProdWipTaskMaterialCO;
import com.erp.prod.wip.service.ProdWipTaskMaterialService;

@Controller
@RequestMapping("/web/prodWipTaskMaterial")
public class ProdWipTaskMaterialWebController extends ControllerSupport{
    
    //日志处理
    private Logger logger = LoggerFactory.getLogger(ProdWipTaskMaterialWebController.class);
    
    //服务层注入
    @Autowired
    private ProdWipTaskMaterialService prodWipTaskMaterialService;
    @Autowired
    private ProdWorkProcedureService prodWorkProcedureService;
    @Autowired
    private DatasetCommonService datasetCommonService;
    @Autowired
    private MasterDataCommonService masterDataCommonService;
    @Autowired
    @Qualifier("invInputHeadServiceCommon")
    private InvInputHeadService invInputHeadService;
    @Autowired
    @Qualifier("invOutputHeadServiceCommon")
    private InvOutputHeadService invOutputHeadService;
    
    @Override
    public String getExceptionRedirectUrl() {
        return this.getFormatRedirectUrl("getProdWipTaskMaterialList");
    }
    
    
    
    /**
     * 
     * @description 查询数据列表
     * @date 2020-12-07 20:41:57
     * @author 
     * @param pages
     * @param prodWipTaskMaterialCO
     * @param model
     * @return String
     *
     */
    @RequestMapping("getProdWipTaskMaterialList")
    public String getProdWipTaskMaterialList(Pages pages, ProdWipTaskMaterialCO prodWipTaskMaterialCO, Model model) {
        //分页查询数据
        if(pages.getPage()==0) {
            pages.setPage(1);
        }
        pages.setMax(100);
        
        //分页查询数据
        List<ProdWipTaskMaterial> prodWipTaskMaterialList = this.prodWipTaskMaterialService.getWipTaskMaterialListByTaskCode(pages, prodWipTaskMaterialCO);
        
        //获取当前任务已领取物料的列表
        List<ProdInvOutputLine> prodInvOutputLineList = this.invOutputHeadService.getProdInvOutputLineList(prodWipTaskMaterialCO.getTaskCode());
        //获取当前任务以退回物料的列表
        List<ProdInvInputLine> prodInvInputLineList = this.invInputHeadService.getProdInvInputLineList(prodWipTaskMaterialCO.getTaskCode());
        
        //循环物料需求行
        for(ProdWipTaskMaterial prodWipTaskMaterial: prodWipTaskMaterialList) {
            //获取物料信息
            MdMaterial mdMaterial = this.masterDataCommonService.getMdMaterialInfoCache(prodWipTaskMaterial.getMaterialCode());
            
            prodWipTaskMaterial.setMaterialUnit(this.datasetCommonService.getMaterialUnit().get(mdMaterial.getMaterialUnit()));
            prodWipTaskMaterial.setMaterialStandard(mdMaterial.getStandard());
            prodWipTaskMaterial.setMaterialName(mdMaterial.getMaterialName());
            
            //获取工序信息
            try {
                if(StringUtils.isNotBlank(prodWipTaskMaterial.getProcedureCode())) {
                    ProdWorkProcedure prodWorkProcedure = this.prodWorkProcedureService.getDataObject(prodWipTaskMaterial.getProcedureCode());
                    prodWipTaskMaterial.setProcedureName(prodWorkProcedure.getProcedureName());
                }
            }catch(Exception e) {
            }
            
            //获取领料的数量
            for(ProdInvOutputLine prodInvOutputLine: prodInvOutputLineList) {
                if(prodInvOutputLine.getMaterialCode().equals(prodWipTaskMaterial.getMaterialCode())&&prodInvOutputLine.getOutputSourceLineCode().equals(prodWipTaskMaterial.getTaskMaterialCode())) {
                    prodWipTaskMaterial.setGetQuantity(prodInvOutputLine.getOutputQuantity());
                    break;
                }
            }
            
            //获取退料的数量
            for(ProdInvInputLine prodInvInputLine: prodInvInputLineList) {
                if(prodInvInputLine.getMaterialCode().equals(prodWipTaskMaterial.getMaterialCode())&&prodInvInputLine.getInputSourceLineCode().equals(prodWipTaskMaterial.getTaskMaterialCode())) {
                    prodWipTaskMaterial.setReturnQuantity(prodInvInputLine.getInputQuantity());
                    break;
                }
            }
        }
          
        

        
        //获取单位
        Map materialUnitMap = this.datasetCommonService.getMaterialUnit();
        
        //页面属性设置
        model.addAttribute("prodWipTaskMaterialList", prodWipTaskMaterialList);
        model.addAttribute("pages", pages);
        model.addAttribute("materialUnitMap", materialUnitMap);
        
        return "prodWipTask/tab/materialLineTab";
    }
    
    
    
    /**
     * 
     * @description 查询单条数据
     * @date 2020-12-07 20:41:57
     * @author 
     * @param prodWipTaskMaterial
     * @param model
     * @return String
     *
     */
    @RequestMapping("getProdWipTaskMaterial")
    public String getProdWipTaskMaterial(ProdWipTaskMaterial prodWipTaskMaterial, Model model) {
        //查询要编辑的数据
        if(prodWipTaskMaterial!=null&&prodWipTaskMaterial.getTaskMaterialId()!=null) {
            prodWipTaskMaterial = this.prodWipTaskMaterialService.getDataObject(prodWipTaskMaterial.getTaskMaterialId());
        }
        
        //获取单位
        Map materialUnitMap = this.datasetCommonService.getMaterialUnit();
        
        //页面属性设置
        model.addAttribute("prodWipTaskMaterial", prodWipTaskMaterial);
        model.addAttribute("materialUnitMap", materialUnitMap);
        
        return "prodWipTask/pop/addMaterialLineModal";
    }
    
    
    
    /**
     * 
     * @description 编辑数据
     * @date 2020-12-07 20:41:57
     * @author 
     * @param prodWipTaskMaterial
     * @param bindingResult
     * @param model
     * @return String
     *
     */
    @RequestMapping("editProdWipTaskMaterial")
    @ResponseBody
    public String editProdWipTaskMaterial(@Valid ProdWipTaskMaterial prodWipTaskMaterial, BindingResult bindingResult, Model model, RedirectAttributes attr) {
        try {
            //对当前编辑的对象初始化默认的字段
            if(prodWipTaskMaterial.getTaskMaterialId()==null) {
                prodWipTaskMaterial.setTaskMaterialCode(SnowFlake.getConcurrentId().toString());
            }
            
            //保存编辑的数据
            this.prodWipTaskMaterialService.insertOrUpdateDataObject(prodWipTaskMaterial);
            
            return "{\"result\":\"success\"}";
        }catch(Exception e) {
            return "{\"result\":\"error\"}";
        }
    }
    
    
    
    /**
     * 
     * @description 删除数据
     * @date 2020-12-07 20:41:57
     * @author 
     * @param prodWipTaskMaterial
     * @return String
     *
     */
    @RequestMapping("deleteProdWipTaskMaterial")
    @ResponseBody
    public String deleteProdWipTaskMaterial(ProdWipTaskMaterial prodWipTaskMaterial, RedirectAttributes attr) {
        try {
            //删除数据前验证数据
            if(prodWipTaskMaterial!=null&&prodWipTaskMaterial.getTaskMaterialId()!=null) {
                //删除数据
                this.prodWipTaskMaterialService.deleteDataObject(prodWipTaskMaterial);
            }
            
            return "{\"result\":\"success\"}";
        }catch(Exception e) {
            return "{\"result\":\"error\"}";
        }
    }
    
    
    
    /**
     * 
     * @description 创建车间任务物料需求（异步）
     * @date 2020年12月21日
     * @author dongbin
     * @param taskCode
     * @return
     * @return String
     *
     */
    @RequestMapping("createWipTaskMaterielAjax")
    @ResponseBody
    public String createWipTaskMaterielAjax(String taskCode) {
        return this.prodWipTaskMaterialService.createWipTaskMateriel(taskCode);
    }
}
